Ext.apply(Ext.form.Field.prototype,{
	 required : false,
	 appendText:'',
	 onBlur : function(){
        this.beforeBlur();
        if(!Ext.isOpera && this.focusClass){ // don't touch in Opera
            this.el.removeClass(this.focusClass);
        }
        this.hasFocus = false;
        if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
			if(this.inputType!="file"){
				this.validate();
			}else{
				if(this.getValue()!=""){
					this.validate();
				}				
			}
        }
        var v = this.getValue();
        if(String(v) !== String(this.startValue)){
            this.fireEvent('change', this, v, this.startValue);
        }
        this.fireEvent("blur", this);
    },	
	 afterRender : function(ct){
        Ext.form.Field.superclass.afterRender.call(this);
        
		
        this.initEvents();
        this.on("valid",this.valid,this);
        //如果设置必填标志为true
      	if(this.required == 'false'){
      		this.required = false;
      	}else if(this.required == 'true'){
      		this.required = true;
      	}
      if(this.appendText.length > 0){
      	 this.wrap = this.el.wrap({cls: "x-form-field-wrap"});
      	 this.displayDom = this.wrap.createChild({tag:'span',html:this.appendText});
      	 if(!this.width){
            this.wrap.setWidth(this.el.getWidth()+this.displayDom.getWidth());
        }
      }
      if(this.required){
       		//创建一个结点
            this.requiredIcon = ct.createChild({style:'position:absolute;color:red;display:block;font-family:"宋体","新宋体",Arial;font-size:14px;font-style:normal;',html:'*'});
            this.alignRequeiredIcon();
            this.on('resize', this.alignRequeiredIcon, this);
            //this.on('resize', this.alignRequeiredIcon(), this);
        }
      //增加上传控件只读 
	  if(this.inputType == "file"){
		//this.el.dom.onkeydown = this.setReadOnly;
	  }	
    },
    
    //屏蔽键盘输入
    //setReadOnly:function(){
	//   if(event.keyCode!=9 && (event.keyCode<35 || event.keyCode>40)) return false;
	//},

    alignRequeiredIcon:function(){
    	this.requiredIcon.alignTo(this.wrap || this.el, 'tl-tr', [2, 0]);
    },
    alignErrorIcon : function(){
     	if(this.required){
        	this.requiredIcon.hide();
        }
        this.errorIcon.alignTo(this.wrap || this.el, 'tl-tr', [2, 0]);
    },
    valid:function(){
    	if(this.required){
    		this.requiredIcon.show();
    	}
    }
    
});

Ext.apply(Ext.form.TriggerField.prototype,{
	 alignErrorIcon : function(){
     	if(this.required){
        	this.requiredIcon.hide();
        }
        this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
    },
	alignRequeiredIcon:function(){
        this.requiredIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);	
	}
});







Ext.namespace("VAS");


VAS.DynamicFormPanel = function(config) {
	this.config = config;
	this.config.items = [];
    VAS.DynamicFormPanel.superclass.constructor.call(this,config);
}


Ext.extend(VAS.DynamicFormPanel,Ext.form.FormPanel,{

	id : 'dyncForm',
	
	title : 'Form',
	
	frame : true,
	
	collapsible :true,
	
	width : 600,
	
	height :300,
	
	url :'',
    /**
     * 最大列数, 默认值是4
     */
    maxCols : 4,
    /**
     * 列宽, 默认值是每列25%. 另外, 所有列的总宽度为组件面板(cpWidth)的宽度
     */
    cw : .25,
    /**
     * LabelWidth, 默认值是60
     */
    lw : 60,
    /**
     * 指定每列的label宽度, 以空格分隔(如: "50 50 60 100", 指定四列的label宽度, 若指定label宽度数不够总列数, 则取默认宽度lw的值)
     */
    diffLabelWidth : null,
    
    
    diffColumnWidth : null,   
    /**
     * 组件面板的宽度
     */
    cpWidth : 1,
    /**
     * 封装待布局的组件数组
     */
    cmps :[],
    /**
     * 封装待布局的Button数组
     */
    btns:[],
    
    
	onRender : function(ct, position) {
		VAS.DynamicFormPanel.superclass.onRender.call(this, ct, position);
	},

    initLayout : function() {
    	if (this.maxCols) {
    		this.cw= this.cpWidth / this.maxCols;
    	}
    	
        // 定义组件面板, 用于装提供查询条件的组件
        var cmpsPanel = new Ext.Panel({
            columnWidth : this.cpWidth,
            layout : 'column'
        });
        
        var btLen = "margin:"+ parseInt((this.height - (this.cmps.length / 2 == 0 ? this.cmps.length / 2 : this.cmps.length / 2 + 1) * 35) * 0.85) +"px 0px 0px 0px";
       // alert(btLen);
       // var btLen = "margin:0px 0px 50px 0px";
        
        // 定义按钮面板, 用于装查询, 清除之类的按扭
        var btnsPanel = new Ext.Panel({
        	columnWidth : this.cpWidth,
            buttonAlign : 'center',
            style : btLen
        });
        
        // 设置查询主面板布局, 并将组件面板和按钮面板放入其中
        this.layout = 'form';
        this.items = [{
            xtype : 'panel',
            layout : 'column',
            items : [cmpsPanel,btnsPanel]
        }];

        // 若设置了diffLabelWidth配置项, 则dlw存放每列的指定宽度
        var dlw = null;
        if (this.diffLabelWidth != null) {
            dlw = this.diffLabelWidth.split(' ');
        }

        var dcw = null;
        if (this.diffColumnWidth != null) {
            dcw = this.diffColumnWidth.split(' ');
        }

        // 组件面板中组件的布局处理
        for (var i = 0; i < this.maxCols; i++) {
            // 每列的子Panel面板定义
            var panel = new Ext.Panel({
                columnWidth : dcw != null && dcw[i] != undefined ? parseFloat(dcw[i]) : this.cw,
                labelWidth: dlw != null && dlw[i] != undefined ? parseInt(dlw[i]) : this.lw,
                layout: 'form'
            });
            
            // 添加控件到子Panel中
            for (var j = i; j < this.cmps.length; j += this.maxCols) {
                panel.add(this.cmps[j]);
            }
            cmpsPanel.add(panel);
        }
        
        // 放置按钮到按钮面板中
        
		var _this = this;
        for (var i = 0; i <this.btns.length; i++) {
        	if (this.btns[i].action == "save") {
	        	this.btns[i].handler = function() {
		    		if(_this.form.isValid()){
			    		_this.form.submit({
				              url: this.url,
							  waitTitle: '正在提交',
				              waitMsg: '正在向服务器发送请求',
				              clientValidation: true,
							  success :function(form,action){
									Ext.Msg.alert('提示','提交保存成功!',function(){ 
									});
							  },
							  failure :function(form,action){
									if(action.failureType == Ext.form.Action.CLIENT_INVALID){
										Ext.Msg.alert('提示','输入的信息不符合规格，请按照提示重新输入！');
									}else{
										alertFailure(action.result.data);
									}
							 }		
			            });
		    		}
	        	};
        	} else if (this.btns[i].action == "reset") {
	        	this.btns[i].handler = function() {
	        		_this.form.reset();
	        	};
        	}
            btnsPanel.addButton(this.btns[i]);
        }
    },
   	
    
    
    
	initComponent : function() {
		var _data = SyncAjax(this.url,{});
		this.cmps = _data.fields;		
		this.btns = _data.buttons;
	   // 初始化布局面板
	   this.initLayout();
	   VAS.DynamicFormPanel.superclass.initComponent.call(this);
	}
	
});

Ext.reg('dynamicformpanel', VAS.DynamicFormPanel);


